package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.exception.ServiceSException;
import com.woniu.car.Util.GetAllCategory;
import com.woniu.car.Util.NumberCreate;
import com.woniu.car.domain.TProductCategory;

import com.woniu.car.mapper.TProductCategoryMapper;
import com.woniu.car.param.AddCategoryChildParam;
import com.woniu.car.param.AddGoodsCategoryParam;
import com.woniu.car.service.TProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chestnuts
 * @since 2021-06-21
 */
@Slf4j
@Service
public class TProductCategoryServiceImpl extends ServiceImpl<TProductCategoryMapper, TProductCategory> implements TProductCategoryService {

    @Resource
    private TProductCategoryMapper productCategoryMapper;


    @Override
    public ResponseEntity addProCategory(AddGoodsCategoryParam addGoodsCategoryParam) throws ServiceSException {
        log.info("添加一个新的分类,{}",addGoodsCategoryParam.getCategoryName());
        QueryWrapper<TProductCategory> query = new QueryWrapper<>();
        query.eq("category_name",addGoodsCategoryParam.getCategoryName());
        TProductCategory tProductCategory = productCategoryMapper.selectOne(query);
        if(tProductCategory != null){
            throw new ServiceSException("该分类已存在,请勿重复添加");
        }
        TProductCategory category = new TProductCategory();
        BeanCopyUtils.copyProperties(addGoodsCategoryParam,category);
        String gategoryNO = NumberCreate.getGategoryNO();
        category.setCategoryNo(gategoryNO);
        category.setCreateTime(Long.toString(System.currentTimeMillis()));
        category.setCreateNo("2568948562");
        int i = productCategoryMapper.insert(category);
        TProductCategory category1 = productCategoryMapper.selectOne(query);
        return i>0?new ResponseEntity().setCode(200).setMsg("分类添加成功").setObject(category1):new ResponseEntity().setCode(200).setMsg("添加失败");
    }

    @Override
    public ResponseEntity selectAllCateGory() {
        List<TProductCategory> category = productCategoryMapper.selectList(null);
        List<TProductCategory> allCategory = GetAllCategory.listWithTree(category);
        return new ResponseEntity().setObject(allCategory).setCode(200).setMsg("查找成功");
    }

    @Override
    public ResponseEntity getLevel() {
        QueryWrapper<TProductCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("category_level", 1);
        List<TProductCategory> list = productCategoryMapper.selectList(wrapper);
        return new ResponseEntity().setObject(list).setCode(200).setMsg("查找成功");
    }

    @Override
    public ResponseEntity addParentCategory(String categoryName) {
        TProductCategory tProductCategory = new TProductCategory();
        String gategoryNO = NumberCreate.getGategoryNO();
        tProductCategory.setCategoryNo(gategoryNO);
        tProductCategory.setParentId(0);
        tProductCategory.setCategoryLevel(1);
        tProductCategory.setCreateNo(NumberCreate.getGategoryNO());
        String s = Long.toString(System.currentTimeMillis());
        tProductCategory.setCreateTime(s);
        tProductCategory.setCategoryName(categoryName);
        int insert = productCategoryMapper.insert(tProductCategory);
        TProductCategory gategory = productCategoryMapper.selectOne(new QueryWrapper<TProductCategory>().eq("create_time", s));
        return new ResponseEntity().setObject(gategory).setMsg("添加一级分类成功").setCode(200);
    }

    @Override
    public ResponseEntity addChildCategory(AddCategoryChildParam addCategoryChildParam) {
        List<String> categoryNames = addCategoryChildParam.getCategoryNames();
        Integer parentId = addCategoryChildParam.getParentId();
        for(String i:categoryNames){
            TProductCategory category = new TProductCategory();
            String gategoryNO = NumberCreate.getGategoryNO();
            category.setCategoryNo(gategoryNO);
            category.setParentId(parentId);
            category.setCategoryLevel(2);
            category.setCreateNo(NumberCreate.getGategoryNO());
            String s = Long.toString(System.currentTimeMillis());
            category.setCreateTime(s);
            category.setCategoryName(i);
            int insert = productCategoryMapper.insert(category);
        }
        return new ResponseEntity().setMsg("添加子集分类成功").setCode(200);
    }


}
